'use strict';

/* Controllers */
app.controller('houseOwnerLandLordRentAmountListCtl', function($scope,$compile,ngDialog,uiLoad,$state,user) {
    var data = angular.copy($scope.ngDialogData, {});
    var rentTypeObj = {
        0: "年付",
        1: "季付",
        2: "月付"
    };
    var applyTypeObj = {
        0: "支付包",
        1: "微信",
        2: "现金"
    };
    $scope.checkType = {};
    $scope.houseType = {};
    $scope.payStatus = {};
    $scope.checkTypeArray = [{ "name": "全部", "value": "-1" }, { "name": "未收租", "value": "0" }, { "name": "已收租", "value": "1" }];
    if($scope.queryParams){

    }
    $scope.queryParams = {
        'payStatus': $scope.payStatus.selected,
        'startTime': $scope.startTime,
        'endTIme': $scope.endTIme,
        'nameCode': $scope.nameCode
    }
    $scope.$watch('_contractNo', function(newValue, oldValue) {
         $scope.queryParams.contractNo = newValue;
    })
    $scope.$watch('_tenantsName', function(newValue, oldValue) {
         $scope.queryParams.tenantsName = newValue;
    })
    $scope.$watch('_tenantsPhone', function(newValue, oldValue) {
         $scope.queryParams.tenantsPhone = newValue;
    })
    $scope.queryUser = function(){
        if($scope.queryParams.payStatus && $scope.queryParams.payStatus.selected){
            var payStatusCache = $scope.queryParams.payStatus.selected;
            if($scope.queryParams.payStatus.selected){
                $scope.queryParams.payStatus = $scope.queryParams.payStatus.selected.value
            }
            $scope.dataTable.fnClearTable();
            $scope.queryParams.payStatus = {};
            $scope.queryParams.payStatus.selected = {};
            $scope.queryParams.payStatus.selected = payStatusCache;
        }else{
            $scope.dataTable.fnClearTable();
        }
    };
    $scope.tableLoanDatas = {
        "sAjaxSource": "3009",
        "aoColumns":  [
            {
                "mDataProp": "contractNo",
                "sTitle": "合同编号",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "tenantsName",
                "sTitle": "租客姓名",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "areaName",
                "sTitle": "小区名称",
                "sDefaultContent": "",
                "sClass": "center"
            },{
                "mDataProp": "roomCode",
                "sTitle": "房间号",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "leaseCycle",
                "sTitle": "租赁周期",
                "sDefaultContent": "",
                "sClass": "center rent-circle"
            },{
                "mDataProp": "houseRent",
                "sTitle": "实际租金",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "rentPayTypeStr",
                "sTitle": "支付方式",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "planStatusStr",
                "sTitle": "当前交租状态",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "operate",
                "sTitle": "操作",
                "sDefaultContent": "",
                "sClass": "center handle-td"
            }
        ],
        "fnRowCallback": function(nRow, aData, iDisplayIndex ) {
               var btnStr = "";
               btnStr += "<a href='javascript:;' class='btn btn-yellow' ng-click='godetail(\"" + aData.contractId + "\")' style='margin-right:10px;'>查看</a>";
        //     // btnStr += "<a href='javascript:;' class='btn btn-yellow disabled' style='margin-right:10px;' ng-click='decorateWithdraw(\"" + aData.decorateId + "\")'>提现</a>";
        //     // btnStr += "<a href='javascript:;' class='btn btn-yellow' ng-click='cancelReturnProOrder(" + aData.houseId + "," + aData.orderId +")' style='margin-right:10px;' >补件</a>";
               console.log(aData);
               function time(mydate){
                  var str = mydate.substring(0,10);
                  var dataArr = str.split('-');
                  var timeStr = dataArr[0]
                    +"年"+dataArr[1]+"月"+dataArr[2]
                    +"日";
                    return timeStr;
                }
               $(nRow).find(".handle-td").append($compile(btnStr)($scope));
               var leaseCycle = time(aData.leaseStartTime) + " - " + time(aData.leaseEndTime);
               $(nRow).find(".rent-circle").empty().append(leaseCycle);

        },
        "fnDrawCallback": function(oSettings) {
            $scope.payStatusValue = $scope.queryParams.payStatus;
            if ("number" == typeof $scope.payStatusValue) {
                $scope.checkTypeArray.map(function(ele, index) {
                    if ($scope.payStatusValue == ele.value) {
                        $scope.payStatus.selected = ele;
                    }
                })
            }
        },
        "fnServerParams": function(aoData) {
            if($scope.payStatus && $scope.payStatus.selected){
                $scope.queryParams.payStatus = $scope.payStatus.selected.value
            }
            aoData.push({
                'name': 'queryParams',
                'value': $scope.queryParams
            });
            return aoData;
        }
    };


    $scope.dataTable = undefined;

    // 回调函数,返回实例化的jquery 插件对象
    $scope.initFinish = function(dataTable) {
        $scope.dataTable = dataTable;
    }
    $('#example thead').on('click', 'th', function() {
        // var data = $('#example').DataTable().row( this ).data();
        // alert('You clicked on  row');
    });
    $('#example tbody').on('click', 'span#edit', function() {
        var data = $('#example').DataTable().row($(this).parents('tr'));
        alert("查看修改：" + data[0]);
    });
    $('#example tbody').on('click', 'span#del', function() {
        // var data = $('#example').DataTable().row($(this).parents('tr')).data();
    });

    $scope.godetail = function (orderId) {
      ngDialog.openConfirm({
          template: 'views/houseOwner/landLordRentAmountDetail.html',
          resolve: {
            load: function() {
              return uiLoad.load(['js/controllers/houseOwner/landLordRentAmountDetail.js'])
            }
          },
          data:{
            orderId:orderId
          },
          scope: $scope,
          width: 1200,
          // height: 700
        }).then(function(data) {

        }, function(reason) {

        });

    }

});
